package com.example.lib_data.base.activity

import android.Manifest
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.jaeger.library.StatusBarUtil


abstract class BaseActivity: AppCompatActivity() {
    private var mPostNotificationLauncher:ActivityResultLauncher<String>? = null
    private var hasPostNotificationPermission  = false

    private var _binding: ViewBinding? = null
    val mBinding:ViewBinding?
        get() = _binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val resID = getLayoutId()
        if (resID != 0)
            setContentView(resID)

        _binding = getLayoutBinding()
        if (_binding != null)
            setContentView(_binding!!.root)

        //StatusBarUtil.setTransparent(this)
        initUI()

        initPostNotification()
    }

    private fun initPostNotification(){
        mPostNotificationLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){
            hasPostNotificationPermission = it
        }
    }

    /**
     * 请求PostNotification权限
     */
    fun requestPostNotification(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (!hasPostNotificationPermission) {
                mPostNotificationLauncher?.launch(Manifest.permission.POST_NOTIFICATIONS)
            }
        }
    }

    /**
     * 初始化UI
     */
    open fun initUI() {}

    /**
     * 获取布局的资源id
     */
    open fun getLayoutId():Int = 0

    /**
     * 返回绑定类对象
     */
    open fun getLayoutBinding(): ViewBinding? = null

    fun navigateToActivity(activityKClass: Class<*>, enter:Int = 0, exit:Int = 0){
        val intent = Intent(this, activityKClass)
        startActivity(intent)
        if (enter != 0 && exit != 0) overridePendingTransition(enter,exit)
    }
}